#/usr/bin/env python
# -*- coding: UTF-8 -*-

'''
Copyright (C) 2010 dbzhang800@gmail.com
All rights reserved.

2010.07.23
''' 
import sys
import os
import platform
import shutil
import subprocess
import re

def setMunroPath(munrodirs):
    '''set menuro path, if munro is empty, only add script dir to path.'''
    binpath = [os.path.abspath(__file__),]
    if munrodirs:
        if isinstance(munrodirs, str) or isinstance(munrodirs, unicode):
            munrodirs = [munrodirs]

        for munrodir in munrodirs:
            if platform.system() == 'Windows' and isinstance(munrodir, unicode):
                # convert unicode to local charset
                munrodir = munrodir.encode(sys.getfilesystemencoding( ))
            binpath.append(munrodir)  #
            binpath.append(os.path.join(munrodir, 'sofem', 'bin'))
            binpath.append(os.path.join(munrodir, 'optics', 'bin'))
            binpath.append(os.path.join(munrodir, 'source', 'bin'))
            binpath.append(os.path.join(munrodir, 'refine', 'bin'))
    binpath.append(os.environ['PATH'])

    if platform.system() == 'Windows':
        os.environ['PATH']= ';'.join(binpath)
    else:
        os.environ['PATH']= ':'.join(binpath)

def callMunroPrograms(program, filename=""):
    '''call external program. if filename was not expty, give the file to the program'''
    try:
        if filename[-4] == '.' and (filename[-3:].lower() in ['dat', 'res', 'axv', 'axb', 'axd']):
            filename = filename[:-4]
    except:
        pass
    if filename:
        res = subprocess.call(program + ' "' + filename + '"', shell=True)
    else:
        res = subprocess.call(program, shell=True)
    print res,  program,  filename
    return res

class MunroMeshTableRect:
    def __init__ (self, left=0, right=0, top=0, bottom=0):
        self._left = left
        self._right = right
        self._top = top
        self._bottom = bottom
    def __str__(self):
        return str(self.value())
    def value(self):
        return (self._left, self._right, self._top, self._bottom)
    def left(self):
        return self._left
    def right(self):
        return self._right
    def top(self):
        return self._top
    def bottom(self):
        return self._bottom

if __name__ == "__main__":
    setMunroPath(r"E:\opee pack-0.3.0.1\data\munro")
    setMunroPath(u"F:\\整理\\ziliao\\linux\\work\\munro\\munro")
    print MunroMeshTableRect(0,0,0,1)
    callMunroPrograms("aber")
